import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;



/**
 * @author Mathieu Maret
 * 
 * This class allow the EasyAwarness to communicate with the user.
 * It will display a screen with a question and a input box
 *
 */
public class UserInput extends EasyAwarenessInput {
	private Display ecran;
	private Form menu;
	private Form next;
	private String varName;
	private EasyAwarenessManager manager;
	
	/**
	 * Create a new UserInput
	 * @param ecran The Display of the midlet
	 * @param nextForm A Form that will be displayed after the user press the Ok button (could be null)
	 * @param menuName name of the windows
	 * @param desc description of the input asked (the text message that will be ask to the user)
	 * @param label name of the parameter asked 
	 * @param text initial content of the input (could be null) 
	 * @param maxSize maximum size of the input
	 * @param constraints type of the input (see TextField class in the MID profile for more precision)
	 */
	public UserInput(Display ecran, Form nextForm, String menuName, String desc, String label, String text, int maxSize, int constraints){
		this.ecran = ecran;
		next = nextForm;
		varName = label;
		menu = new Form(menuName);
		menu.append(desc+"\n");
		menu.append(new TextField(label,text, maxSize,constraints));
		menu.addCommand(new Command("Ok", Command.OK,0));
		menu.setCommandListener(new SimpleCommandListener());
		manager = this.getManager();
		name = "UserInput";
	}
	
	

	/* (non-Javadoc)
	 * @see EasyAwarenessInput#Research()
	 */
	public void Research() {
		ecran.setCurrent(menu);
		
	}

	public class SimpleCommandListener implements CommandListener{

		public void commandAction(Command arg0, Displayable arg1) {
			switch (arg0.getCommandType()){
			case Command.OK:
				String retour = ((TextField)menu.get(1)).getString();
				Message m = new Message("<"+varName+">"+retour+"</"+varName+">");
				m.setSourceType(EasyAwarenessInput.USER);
				manager.messageReceive(m);
				if(next != null){
					ecran.setCurrent(next);
				}
				break;
			
			}
			
		}
		
	}

	public Form getNext() {
		return next;
	}
	/**
	 * Set the form that will be displayed after the user enter the parameter
	 * @param next
	 */
	public void setNext(Form next) {
		this.next = next;
	}


}
